/**
 * @description 获取localStorage
 * @param {String} key Storage名称
 * @return string
 */
export const localGet = (key) => {
  const value = window.localStorage.getItem(key);
  try {
    return JSON.parse(window.localStorage.getItem(key));
  } catch (error) {
    return value;
  }
};

/**
 * @description 存储localStorage
 * @param {String} key Storage名称
 * @param {Any} value Storage值
 * @return void
 */
export const localSet = (key, value) => {
  window.localStorage.setItem(key, JSON.stringify(value));
};

/**
 * @description 清除localStorage
 * @param {String} key Storage名称
 * @return void
 */
export const localRemove = (key) => {
  window.localStorage.removeItem(key);
};

/**
 * @description 清除所有localStorage
 * @return void
 */
export const localClear = () => {
  window.localStorage.clear();
};

/**
 * @description 获取浏览器默认语言
 * @return string
 */
export const getBrowserLang = () => {
  let browserLang = navigator.language
    ? navigator.language
    : navigator.browserLanguage;
  let defaultBrowserLang = "";
  if (
    browserLang.toLowerCase() === "cn" ||
    browserLang.toLowerCase() === "zh" ||
    browserLang.toLowerCase() === "zh-cn"
  ) {
    defaultBrowserLang = "zh";
  } else {
    defaultBrowserLang = "en";
  }
  return defaultBrowserLang;
};

/**
 * @description 获取需要展开的 subMenu
 * @param {String} path 当前访问地址
 * @returns array
 */
export const getOpenKeys = (path) => {
  let newStr = "";
  let newArr = [];
  let arr = path.split("/").map((i) => "/" + i);
  for (let i = 1; i < arr.length - 1; i++) {
    newStr += arr[i];
    newArr.push(newStr);
  }
  return newArr;
};

/**
 * @description 递归查询对应的路由
 * @param {String} path 当前访问地址
 * @param {Array} routes 路由列表
 * @returns array
 */
export const searchRoute = (path, routes = []) => {
  let result = {};
  for (let item of routes) {
    if (item.path === path) return item;
    if (item.children) {
      const res = searchRoute(path, item.children);
      if (Object.keys(res).length) result = res;
    }
  }
  return result;
};

/**
 * @description 递归查询对应的路由
 * @param {String} identifier 当前访问地址的标识符
 * @param {Array} routes 路由列表
 * @returns array
 */
export const searchRouteByIdentifier = (identifier, routes = []) => {
  let result = {};
  for (let item of routes) {
    if (item.identifier === identifier) return item;
    if (item.children) {
      const res = searchRouteByIdentifier(identifier, item.children);
      if (Object.keys(res).length) result = res;
    }
  }
  return result;
};

/**
 * @description 递归当前路由的 所有 关联的路由，生成面包屑导航栏
 * @param {String} path 当前访问地址
 * @param {Array} menuList 菜单列表
 * @returns array
 */
export const getBreadcrumbList = (path, menuList = []) => {
  let tempPath = [];
  try {
    const getNodePath = (node) => {
      tempPath.push(node);
      // 找到符合条件的节点，通过throw终止掉递归
      if (node.path === path) {
        throw new Error("GOT IT!");
      }
      if (node.children && node.children.length > 0) {
        for (let i = 0; i < node.children.length; i++) {
          getNodePath(node.children[i]);
        }
        // 当前节点的子节点遍历完依旧没找到，则删除路径中的该节点
        tempPath.pop();
      } else {
        // 找到叶子节点时，删除路径当中的该叶子节点
        tempPath.pop();
      }
    };
    for (let i = 0; i < menuList.length; i++) {
      getNodePath(menuList[i]);
    }
  } catch (e) {
    return tempPath.map((item) => item.title);
  }
};

/**
 * @description 双重递归 找出 所有面包屑生成对象存到 redux 中，就不用每次都去递归查找了
 * @param {String} menuList 当前菜单列表
 * @returns object
 */
export const findAllBreadcrumb = (menuList = []) => {
  let handleBreadcrumbList = {};
  const loop = (menuItem) => {
    // 下面判断代码解释 *** !item?.children?.length   ==>   (item.children && item.children.length > 0)
    if (menuItem?.children?.length)
      menuItem.children.forEach((item) => loop(item));
    else
      handleBreadcrumbList[menuItem.path] = getBreadcrumbList(
        menuItem.path,
        menuList
      );
  };
  menuList.forEach((item) => loop(item));
  return handleBreadcrumbList;
};

/**
 * @description 使用递归处理路由菜单，生成一维数组，做菜单权限判断
 * @param {Array} menuList 所有菜单列表
 * @param {Array} newArr 菜单的一维数组
 * @return array
 */
export function handleRouter(routerList = [], newArr = []) {
  routerList.forEach((item) => {
    typeof item === "object" && item.path && newArr.push(item.path);
    item.children &&
      item.children.length &&
      handleRouter(item.children, newArr);
  });
  return newArr;
}

/**
 * @description 使用递归处理路由菜单，生成一维数组，做菜单权限判断，数组中存放pass的路由标识符
 * @param {Array} menuList 所有菜单列表
 * @param {Array} newArr 菜单的一维数组
 * @return array
 */
export function handleRouterByIdentifier(routerList = [], newArr = []) {
  routerList.forEach((item) => {
    typeof item === "object" && item.identifier && newArr.push(item.identifier);
    item.children &&
      item.children.length &&
      handleRouterByIdentifier(item.children, newArr);
  });
  return newArr;
}

/**
 * @description 判断数据类型
 * @param {Any} val 需要判断类型的数据
 * @return string
 */
export const isType = (val) => {
  if (val === null) return "null";
  if (typeof val !== "object") return typeof val;
  else
    return Object.prototype.toString.call(val).slice(8, -1).toLocaleLowerCase();
};

/**
 * @description 对象数组深克隆
 * @param {Object} obj 源对象
 * @return object
 */
export const deepCopy = (obj) => {
  let newObj;
  try {
    newObj = obj.push ? [] : {};
  } catch (error) {
    newObj = {};
  }
  for (let attr in obj) {
    if (typeof obj[attr] === "object") {
      newObj[attr] = deepCopy(obj[attr]);
    } else {
      newObj[attr] = obj[attr];
    }
  }
  return newObj;
};

/**
 * @description 递归查询对应的数据
 * @param {String} id 当前要查询数据的id
 * @param {Array} data 数据列表
 * @returns array
 */
export const searchDataById = (id, data = []) => {
  let result = null;
  for (let item of data) {
    if (item.id === id) return item;
    if (item.children) {
      const res = searchDataById(id, item.children);
      if (res && Object.keys(res).length) result = res;
    }
  }
  return result;
};

// 数组根据index排序
export const arrSort = (arr) => {
  arr.map((item) => {
    if (item.children?.length) {
      item.children = arrSort(item.children);
    }
  });
  return arr.sort((a, b) => {
    return a.index - b.index;
  });
};

// 数字每隔三位增加一个逗号， 字符串递归方法
export function formatNumber(num, chart = ",", length = 3) {
  let result = "";
  let nums = num.toString().split(".");
  let int = nums[0];
  let decmial = nums[1] ? "." + nums[1] : "";
  let index = 0;
  for (let n = int.length - 1; n >= 0; n--) {
    index++;
    result = int[n] + result;
    if (index % length === 0 && n !== 0) {
      result = chart + result;
    }
  }
  return result + decmial;
}
